﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraGrid.Views.Grid;
using QuanLyKho.PhongGiaCong;

namespace QuanLyKho.Control
{
    public partial class Giacong_Phieuyeucau : UserControl
    {
        public Giacong_Phieuyeucau()
        {
            InitializeComponent();
            LoadData();
            SetPropertiesGrid();
            gcID.Visible = false;
            gcID1.Visible = false;
        }

        private void LoadData()
        {
            var table = DataAll.SelectAll("PhieuYeuCau inner join PhongMau_SanPham on PhieuYeuCau.IdSanPham = PhongMau_SanPham.IdSanPham",
                "PhieuYeuCau.[ID],PhongMau_SanPham.[TuNgay],PhongMau_SanPham.[DenNgay],PhongMau_SanPham.[MaHang] as 'MaSanPham',PhieuYeuCau.[NgayYeuCau],PhieuYeuCau.[NguoiYeuCau],PhieuYeuCau.[SoLuong],PhieuYeuCau.[TrangThai],PhieuYeuCau.[NgayChungTu],PhieuYeuCau.[NgayHoachToan],PhieuYeuCau.[SoChungTu],PhieuYeuCau.[GhiChu],PhieuYeuCau.[PhongBan],PhieuYeuCau.[LoaiPhieu]",
                "WHERE (PhieuYeuCau.[TrangThai]= " + Convert.ToInt32(TrangThai.PhieuYeuCau_ChoXuLy) + " OR PhieuYeuCau.[TrangThai]= " + Convert.ToInt32(TrangThai.PhieuYeuCau_HuyDuyet) + " ) AND PhongBan = " + Convert.ToInt32(PhongBan.PhongGiaCong));
            var imconverter = new ImageConverter();
            var columns = new DataColumn("GuiPhieu", typeof(byte[]));
            columns.DefaultValue = imconverter.ConvertTo(Properties.Resources.export_gray, typeof(byte[]));
            table.Columns.Add(columns);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                if (Convert.ToInt32(table.Rows[i]["TrangThai"]) == Convert.ToInt32(TrangThai.PhieuYeuCau_TaoMoi)
                    || Convert.ToInt32(table.Rows[i]["TrangThai"]) == Convert.ToInt32(TrangThai.PhieuYeuCau_HuyDuyet))
                {
                    table.Rows[i]["GuiPhieu"] = imconverter.ConvertTo(Properties.Resources.export, typeof(byte[]));
                }
            }
            gridPhieuYeuCau.DataSource = table;

            gridPhieuYeuCauXuLy.DataSource = DataAll.SelectAll("PhieuYeuCau inner join PhongMau_SanPham on PhieuYeuCau.IdSanPham = PhongMau_SanPham.IdSanPham",
               "PhieuYeuCau.[ID],PhongMau_SanPham.[MaHang] as 'MaSanPham',PhieuYeuCau.[NgayYeuCau],PhieuYeuCau.[NguoiYeuCau],PhieuYeuCau.[SoLuong],PhieuYeuCau.[TrangThai],PhieuYeuCau.[NgayChungTu],PhieuYeuCau.[NgayHoachToan],PhieuYeuCau.[SoChungTu],PhieuYeuCau.[GhiChu],PhieuYeuCau.[PhongBan],PhieuYeuCau.[LoaiPhieu]",
               "WHERE PhieuYeuCau.[TrangThai] != " + Convert.ToInt32(TrangThai.PhieuYeuCau_TaoMoi) + "AND PhongBan = " + Convert.ToInt32(PhongBan.PhongGiaCong));
            gcTuNgay.Visible = false;
            gcDenNgay.Visible = false;
        }

        public void SetPropertiesGrid()
        {
            gvPhieuYeuCau.BestFitColumns();
            gvPhieuYeuCau.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            gvPhieuYeuCau.Appearance.SelectedRow.BackColor = Color.Empty;
            gvPhieuYeuCau.Appearance.SelectedRow.Options.UseBackColor = true;
            gvPhieuYeuCau.OptionsView.ShowGroupPanel = false;
            gvPhieuYeuCau.OptionsView.ColumnAutoWidth = false;
            gvPhieuYeuCau.OptionsView.ShowFooter = true;
            gvPhieuYeuCau.OptionsFind.AlwaysVisible = true;
            gvPhieuYeuCau.Columns["ID"].Visible = false;
            for (int i = 2; i < gvPhieuYeuCau.Columns.Count; i++)
            {
                gvPhieuYeuCau.Columns[i].OptionsColumn.AllowEdit = false;
            }
        }

        private void gvPhieuYeuCau_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiTrangThai(Convert.ToInt32(e.Value));
            }
            if (e.Column.FieldName == "LoaiPhieu")
            {
                e.DisplayText = Common.HienThiLoaiPhieuYeuCau(Convert.ToInt32(e.Value));
            }
        }

        private void gvPhieuYeuCau_DoubleClick(object sender, EventArgs e)
        {
            if (gvPhieuYeuCau.FocusedRowHandle >= 0)
            {
                Giacong_Phieuyeucau_ThemMoi.MaID = gvPhieuYeuCau.Columns["ID"].View.GetRowCellDisplayText(gvPhieuYeuCau.FocusedRowHandle, "ID");
                var lss = new Giacong_Phieuyeucau_ThemMoi();
                lss.ShowDialog();
                LoadData();
                Giacong_Phieuyeucau_ThemMoi.MaID = "0";
            }
        }

        private void gvPhieuYeuCauXuLy_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiTrangThai(Convert.ToInt32(e.Value));
            }
            if (e.Column.FieldName == "LoaiPhieu")
            {
                e.DisplayText = Common.HienThiLoaiPhieuYeuCau(Convert.ToInt32(e.Value));
            }
        }

        private void gvPhieuYeuCauXuLy_DoubleClick(object sender, EventArgs e)
        {
            if (gvPhieuYeuCauXuLy.FocusedRowHandle >= 0)
            {
                Giacong_Phieuyeucau_ThemMoi.MaID = gvPhieuYeuCauXuLy.Columns["ID"].View.GetRowCellDisplayText(gvPhieuYeuCauXuLy.FocusedRowHandle, "ID");
                var lss = new Giacong_Phieuyeucau_ThemMoi();
                lss.ShowDialog();
                LoadData();
                Giacong_Phieuyeucau_ThemMoi.MaID = "0";
            }
        }

        private void btnGuiPhieu_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvPhieuYeuCau.DataSource;
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn gửi phiếu này.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                string message = DataAll.Update("PhieuYeuCau", "TrangThai =" + Convert.ToInt32(TrangThai.PhieuYeuCau_ChoXuLy) + " WHERE ID =" + dataview[gvPhieuYeuCau.FocusedRowHandle]["ID"]);
                if (message.Equals("success"))
                {
                    DataAll.InsertAll("HanhTrinh", dataview[gvPhieuYeuCau.FocusedRowHandle]["MaSanPham"]
                        + "," + Convert.ToInt32(PhongBan.PhongGiaCong)
                        + "," + dataview[gvPhieuYeuCau.FocusedRowHandle]["SoChungTu"]
                        + "," + (dataview[gvPhieuYeuCau.FocusedRowHandle]["LoaiPhieu"].ToString() == "1" ? Convert.ToInt32(TrangThai.PhieuYeuCau_CapThem) : Convert.ToInt32(TrangThai.PhieuYeuCau_TraLai))
                        + "," + Main.UserID
                        + "," + DateTime.Now.ToString("MM/dd/yyyy")
                        + "," + dataview[gvPhieuYeuCau.FocusedRowHandle]["SoLuong"]
                        + "," + Convert.ToDateTime(dataview[gvPhieuYeuCau.FocusedRowHandle]["TuNgay"].ToString()).ToString("MM/dd/yyyy")
                        + "," + Convert.ToDateTime(dataview[gvPhieuYeuCau.FocusedRowHandle]["DenNgay"].ToString()).ToString("MM/dd/yyyy"));
                }
                MessageBox.Show(message.Equals("success") ? "Gửi phiếu thành công" : message, "Message",
                                    MessageBoxButtons.OK,
                                    message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                LoadData();
            }
        }

        private void peGuiPhieu_Click(object sender, EventArgs e)
        {
            var dataview = (DataView)gvPhieuYeuCau.DataSource;
            string a = gvPhieuYeuCau.GetRowCellValue(gvPhieuYeuCau.FocusedRowHandle, "TrangThai").ToString();
            string b = Convert.ToInt32(TrangThai.PhieuYeuCau_TaoMoi).ToString();
            string c = Convert.ToInt32(TrangThai.PhieuYeuCau_HuyDuyet).ToString();
            if (a == b || a == c)
            {
                DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn gửi phiếu này.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    string message = DataAll.Update("PhieuYeuCau", "TrangThai =" + Convert.ToInt32(TrangThai.PhieuYeuCau_ChoXuLy) + " WHERE ID =" + dataview[gvPhieuYeuCau.FocusedRowHandle]["ID"]);
                    if (message.Equals("success"))
                    {
                        DataAll.InsertAll("HanhTrinh", dataview[gvPhieuYeuCau.FocusedRowHandle]["MaSanPham"]
                            + "," + Convert.ToInt32(PhongBan.PhongGiaCong)
                            + "," + dataview[gvPhieuYeuCau.FocusedRowHandle]["SoChungTu"]
                            + "," + Common.HienThiTrangThai(Convert.ToInt32(dataview[gvPhieuYeuCau.FocusedRowHandle]["LoaiPhieu"].ToString()))
                            + "," + Main.UserID
                            + "," + DateTime.Now.ToString("MM/dd/yyyy")
                            + "," + dataview[gvPhieuYeuCau.FocusedRowHandle]["SoLuong"]
                            + "," + Convert.ToDateTime(dataview[gvPhieuYeuCau.FocusedRowHandle]["TuNgay"].ToString()).ToString("MM/dd/yyyy")
                            + "," + Convert.ToDateTime(dataview[gvPhieuYeuCau.FocusedRowHandle]["DenNgay"].ToString()).ToString("MM/dd/yyyy"));
                    }
                    MessageBox.Show(message.Equals("success") ? "Gửi phiếu thành công" : message, "Message",
                                        MessageBoxButtons.OK,
                                        message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                    LoadData();
                }
            }
        }
    }
}
